package com.eboss2.sbs.activity.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocationListener;
import com.eboss2.sbs.R;
import com.eboss2.sbs.myapplication.MyApplication;

public abstract class RootActivity extends Activity implements OnClickListener
{
	/**
	 * 上下文 当进入activity时必须 mContext = this 方可使用，否则会报空指针
	 */
	public Context mContext;
	protected Activity mActivity;
	protected MyApplication mApp;
	private Dialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		mApp = MyApplication.getmInstance();
//		mApp.locationClient.registerLocationListener(this);
	}
	
	@Override
	protected void onDestroy() {
//		mApp.locationClient.unRegisterLocationListener(this);
		super.onDestroy();
	}
	

	
	protected String getTvText(int vid){
		TextView view = (TextView) findViewById(vid);
		CharSequence cs = view.getText();
		if(cs!=null)return cs.toString();
		else return "";
	}
	
	protected void setTvEnable(int vid, boolean boo){
		View view = findViewById(vid);
		if(view!=null){view.setEnabled(boo);}
	}
	



	/**
	 * 文本View
	 */
	public TextView textView(int textview) {
		return (TextView) findViewById(textview);
	}

	protected void setTvText(int vid, String text) {
		TextView view = (TextView) findViewById(vid);
		if(view!=null){view.setText(TextUtils.isEmpty(text) ? "": text);}
	}

	protected void showView(int vid, boolean isShow){
		View view = (View) findViewById(vid);
		if(view!=null){
			view.setVisibility(isShow?View.VISIBLE:View.GONE);
		}
	}

	protected void showLayoutView(int vid, boolean isShow){
		LinearLayout view = (LinearLayout) findViewById(vid);
		if(view!=null){
			view.setVisibility(isShow?View.VISIBLE:View.GONE);
		}
	}

	/**
	 * 文本button
	 */
	public Button button(int id) {
		return (Button) findViewById(id);
	}

	/**
	 * 文本button
	 */
	public ImageView imageView(int id) {
		return (ImageView) findViewById(id);
	}

	/**
	 * 文本editText
	 */
	public EditText editText(int id) {
		return (EditText) findViewById(id);
	}

	public LinearLayout linearLayout(int id) {
		return (LinearLayout) findViewById(id);
	}
	public RelativeLayout relativeLayout(int id) {
		return (RelativeLayout) findViewById(id);
	}
	
//	public void hideByTag(String tag) {
//		final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
//		ArrayList<View> views = (ArrayList<View>) getViewsByTag(viewGroup, tag);
//		for (View _view : views) {
//			_view.setVisibility(View.GONE);
//		}
//	}
//
//	public void showByTag(String tag) {
//		final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
//		ArrayList<View> views = (ArrayList<View>) getViewsByTag(viewGroup, tag);
//		for (View _view : views) {
//			_view.setVisibility(View.VISIBLE);
//		}
//	}
//	
//    private static List<View> getViewsByTag(ViewGroup root, String tag){
//        ArrayList<View> views = new ArrayList<View>();
//        final int childCount = root.getChildCount();
//        for (int i = 0; i < childCount; i++) {
//            final View child = root.getChildAt(i);
//            if (child instanceof ViewGroup) {views.addAll(getViewsByTag((ViewGroup) child, tag));}
//            final Object tagObj = child.getTag();
//            if (tagObj != null && tagObj.equals(tag)) {views.add(child);}
//        }
//        return views;
//    }
    
	public void showDialog() {
		if(dialog==null){
			dialog = new Dialog(mActivity, R.style.Dialog);
			dialog.setCancelable(true);
			dialog.setContentView(R.layout.dialog_dark);
			TextView titleText = (TextView) dialog.findViewById(R.id.dialog_text);
			titleText.setText(getResources().getString(R.string.loading_data));
			dialog.show();
		}
		else if(!dialog.isShowing()){dialog.show();}
	}
	
	public void hideDialog(){
		if(dialog!=null&&dialog.isShowing()){
			dialog.dismiss();
		}
	}
	
}
